package com.licenta.fbApp.server;

import java.util.ArrayList;
import com.licenta.fbApp.shared.Restaurant;

public class Algorithm {
	
	/**
	 * @uml.property  name="minCos"
	 */
	private final double minCos = 0.5; 
	
	public double normalCosine (int[] item1, int[] item2) {
		int len = item1.length;
		int i, sum = 0;
		
		for ( i = 0; i < len; i++) {
			sum += (item1[i] * item2[i] != 0) ? 1 : 0;
		}		
		return (sum/(double)len);
	}
	
	
	public double correlationBased(ArrayList<Restaurant> DB, int r1, int r2, int[][] likes) 
	{
		int i, count = 0;
		double m1 = DB.get(r1).rating, m2 = DB.get(r2).rating, sum1, sum2, sum;
		
		sum1= 0;
		sum2 = 0;
		sum = 0;
		
		for (i = 0; i < likes.length; i++){
			if ( likes[i][r1+1] > 0 && likes[i][r2+1] > 0) {
				sum1 += Math.pow(likes[i][r1+1] - m1, 2);
				sum2 += Math.pow(likes[i][r2+1] - m2, 2);
				sum += Math.abs((likes[i][r1+1] - m1) * (likes[i][r2+1] - m2));
				count++;
			}
		}
			
		if ( count < (likes.length /100D))
			return 0;
		
		if (sum1 * sum2 == 0)
			return 0;
		
		return (sum / (Math.sqrt(sum1 * sum2)));
	}
	
	public double adjustedCosine(double[] userAvg, int r1, int r2, int[][] likes) 
	{
		int i,j, count = 0;
		double sum1, sum2, sum;
		
		sum1= 0;
		sum2 = 0;
		sum = 0;
		
		for (i = 0; i < likes.length; i++){
			if ( likes[i][r1+1] > 0 && likes[i][r2+1] > 0) {
				sum1 += Math.pow(likes[i][r1+1] - userAvg[i], 2);
				sum2 += Math.pow(likes[i][r2+1] - userAvg[i], 2);
				sum += Math.abs((likes[i][r1+1] - userAvg[i]) * (likes[i][r2+1] - userAvg[i]));
				count++;
			}
		}
			
		if ( count < (likes.length /100D))
			return 0;
		
		if (sum1 * sum2 == 0)
			return 0;
		
		return (sum / (Math.sqrt(sum1 * sum2)));
	}
}

